package sk.tuke.lara.masplc.utils;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class CommunicationUtils {
    private static ObjectMapper mapper = new ObjectMapper();

    private CommunicationUtils() {
        // lib pattern
    }

    /**
     * Prevede objekt zpravy na text
     * @param protokol
     * @return
     */
    public static <T> String toJson(T object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Prevede txt zpravu na objekt protokolu
     * @param txt
     * @return
     */
    public static <T> T fromJson(String txt, Class<T> trida) {
        try {
            return mapper.readValue(txt, trida);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
